"""
    分钟行情数据写入
"""
from pathlib import Path
from tqdm import tqdm
import pandas as pd
from data_resource.data_bases import engine, MinuteBars
from utilities.utilities_func import write_to_db
from data_resource.data_bases import engine


def main():
    path = r"D:\高频数据\00～25\data"
    x = [str(file) for file in Path(path).rglob('*.csv')]
    errorlist = []

    for i in x:
        _data = pd.read_csv(i, usecols=lambda col: col != "Unnamed: 0")
        # 判断版式
        if _data.shape[1] == 9:
            _data1 = _data.copy()
            # 标准化股票代码
            _data1['code'] = _data1['code'].apply(lambda l: l[3:])
            # 合并时间字段
            _data1["trading"] = pd.to_datetime(_data1["date"].str.cat(_data1["time"], sep=" "))
            _data1.drop(columns=["date", "time"], inplace=True)
            _data1.rename(columns={'mount': 'amount'}, inplace=True)
        elif _data.shape[1] == 7:
            _code = i.split('.')[-2]
            _data1 = _data.copy()
            _data1['code'] = _code
            try:
                _data1['datetime'] = pd.to_datetime(_data1['datetime'], format='mixed')
            except Exception as e:
                print(e)
                raise ValueError(f"------------------- 时间格式错误，路径：{i}")
            _data1.rename(columns={'datetime': 'trading'}, inplace=True)
        else:
            print(f"------------------- 版式变化，路径：{i}")
            errorlist.append(i)
            continue

        if 'volume' in _data1.columns:
            _data1.rename(columns={'volume': 'volumn'}, inplace=True)

        write_to_db('minute_bars', engine, MinuteBars, _data1)
        print(f"------------ 导入成功：{i}")

    print(f"------------ 导入失败列表：{len(errorlist)}")
    print(errorlist)


# 将爬虫分钟数据加入数据库
def get_minute_bar():
    _sql = """
    select trade_time as trading, ticker as code, open, high, low, close, volumn, amount
    from quant_research.market_1min
    where trade_time > '2025-06-30'
    """
    _data = pd.read_sql(_sql, engine)
    _data['trading'] = pd.to_datetime(_data['trading'], format='mixed')

    write_to_db('minute_bars', engine, MinuteBars, _data)


if __name__ == '__main__':
    # main()
    get_minute_bar()
